473,416 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

adding a menuitem array issue

I've simplified the code to the following. I'm just overlooking something
very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)
What do you think is the cause of the error?

Eric

Nov 20 '05 #1
4 3415
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
I've simplified the code to the following. I'm just overlooking
something very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to
an instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)
What do you think is the cause of the error?

In the mic array, the item with index=3 does not contain a reference to a
MenuItem.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Eric Sabine wrote:
I've simplified the code to the following. I'm just overlooking
something very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))

------ error below ------- here I get an "Object reference not set to
an instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)

What do you think is the cause of the error?


VB.NET is not C# in it's array declarations. The statement "mic = New
MenuItem(3) {}" creates an array of length *4*, that is 0 to 3 *inclusive*.
The error is caused when AddRange tries to add mic(3), which is still
Nothing. You need to change your assignment to "mic = New MenuItem(2) {}".

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #3
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
I've simplified the code to the following. I'm just overlooking something
very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))
'mic(3)' is still 'Nothing'.
------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
D'OH!

Thanks Herfried, Sven, and Armin.

Eric
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c7************@ID-208219.news.uni-berlin.de...
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
I've simplified the code to the following. I'm just overlooking something very simple I'm sure.

Dim mic() As MenuItem
mic = New MenuItem(3) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1

mic(1) = New MenuItem("b")
mic(1).Index = 2

mic(2) = New MenuItem("c")
mic(2).Index = 3

mic(1).MenuItems.Add(mic(2))


'mic(3)' is still 'Nothing'.
------ error below ------- here I get an "Object reference not set to an
instance of an object." error
myProgramsContextMenu1.MenuItems.AddRange(mic)


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: filip stas | last post by:
How do i add references during runtime?
0
by: Claire | last post by:
When I run the following code through on the first round, my sub-menu (branched off mnuOpenPorts menuitem) shows a nice list of ports. When it gets run through on the 2nd pass, the sub menu...
4
by: Claire | last post by:
Sorry Ive added this twice (sortof) but if I'd added an addendum to the first one then this would probably have been ignored. This problem affects a ContextMenu attached to a NotifyIcon object. I...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
6
by: Eric Sabine | last post by:
Basically, the following code creates a menuItem array and tries to use it twice. In the following piece of code, only the line that appears second gets used. The first becomes ignored presumably...
2
by: jack | last post by:
Hello, I need to dynamically add menu items to an existing menu on an MDI form. In the form load, when I create the menu items then add it to the menu control using the Add method, the entire...
1
by: snesbit | last post by:
I have a static ContextMenu already built and it works. I am trying to add a submenu (right arrow with a new menuitem list) dynamically when the popup is created. The new set of menuitems...
2
by: Miky | last post by:
Hi, I'm looking resources or tutorial where I can learn how to add properties to other objects at design time for VB.NEt or C#. I want to reproduce the same effect as when your drop the...
0
by: Jeff | last post by:
hey asp.net 2.0 I want to add a menu item to a menu on my webpage programatically. I've tryed using the code below, but it doesn't add the menu item to the menu. MenuItem menuItem = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.